package com.yuntsg.nnsfcp.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.google.common.base.Joiner;
import com.yuntsg.nnsfcp.config.AliSms;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class SendSms {

    private static final String[] SENDTEMPLATE = {
            "SMS_162198586",  //验证码
            "SMS_158944461",  //接单员进行邮递
            "SMS_158949152", //审核不通过退回
            "SMS_158944459",  //进行审核
            "SMS_158949150",  //指定查新员
            "SMS_158944457", //发送给全部查新员
            "SMS_158944454" //新订单给接单员
    };
    @Resource
    private AliSms aliSms;

    /**
     * 发短信 类型 电话号码 订单号
     *
     * @param type
     * @param phone
     * @param orderId
     * @return
     */
    public String generatorSms(int type, List phone, String orderId) {
        String typetTemplate = "";
        switch (type) {
            case 1:
                typetTemplate = SENDTEMPLATE[0];
                break;
            case 2:
                typetTemplate = SENDTEMPLATE[1];
                break;
            case 3:
                typetTemplate = SENDTEMPLATE[2];
                break;
            case 4:
                typetTemplate = SENDTEMPLATE[3];
                break;
            case 5:
                typetTemplate = SENDTEMPLATE[4];
                break;
            case 6:
                typetTemplate = SENDTEMPLATE[5];
                break;
            case 7:
                typetTemplate = SENDTEMPLATE[6];
                break;
        }

        String parsePhone = Joiner.on(",").join(phone);
        String code = GeneratorCode.generatorCode(); //随机验证码
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", aliSms.getAccessKeyId(), aliSms.getAccessKeySecret());
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", parsePhone);
        request.putQueryParameter("SignName", aliSms.getSignname());
        request.putQueryParameter("TemplateCode", typetTemplate);
        if (type == 1) {
            request.putQueryParameter("TemplateParam", "{\"code\":" + code + "}");
        } else {
            request.putQueryParameter("TemplateParam", "{\"ordersn\":" + orderId + "}");
        }
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
            code = "1234";
        } catch (ClientException e) {
            e.printStackTrace();
            code = "1234";
        }
        return code;
    }
}

